package com.gf.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gf.bean.User;
import com.gf.service.impl.UserServiceImpl;
import com.gf.utils.Const;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/*
 * 查询用户的地址和手机号是否为空的servlet
 * */

@WebServlet(name = "QueryaddressServlet",urlPatterns ="/queryaddress" )
public class QueryaddressServlet extends HttpServlet {
    static UserServiceImpl usermapper ;
    static  {
        usermapper=new UserServiceImpl();
        //创建好用户查询对象

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的内容不缓存，防止出现问题
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();

        int  userid= (int) request.getSession().getAttribute("userid");
        //获取到session中的userid属性
        User user = usermapper.checkUserroomnumberandtel(userid);
        //通过userid去查询用户并且封装成user对象
        String room_number = user.getRoom_number();
        String telephone = user.getTelephone();
        JSONObject backjson = new JSONObject();
        //创建JSON对象
        if (room_number==null||telephone==null){
            //如果判断出用户的地址或者手机号为null直接返回结结果
                response.setStatus(300);
            //设置错误的状态码和信息
                backjson.put("state", 300);
                backjson.put("message", Const.ROOMNUMBERANDTL_NULL);
        }else{
            //设置正确的返回状态码返回正确的响应信息
            response.setStatus(200);
            backjson.put("state", 200);
            backjson.put("message", Const.HAS_ROOMNUMBERANDTL);
        }

        out.println(JSON.toJSON(backjson));

        out.flush();
        out.close();


    }
}
